package city.spring.modules.cardinfo.controller;

import city.spring.modules.cardinfo.entity.CardExpenseRecordEntity;
import city.spring.modules.cardinfo.service.CardExpenseRecordService;
import city.spring.modules.consumptionItem.service.ConsumptionCategoryService;
import city.spring.modules.consumptionItem.service.ConsumptionItemsService;
import city.spring.modules.system.service.OrganizationService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：消费记录
 *
 * @author Panxm
 * @date 2020-04-13 16:01:41
 */
@RestController
@RequestMapping("/card_expense_record")
public class CardExpenseRecordController extends ApiController {
    private final CardExpenseRecordService cardExpenseRecordService;
    private final ConsumptionItemsService consumptionItemsService;
    private final ConsumptionCategoryService consumptionCategoryService;
    private final OrganizationService organizationService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<CardExpenseRecordEntity, ?>> entityOrderFields;

    public CardExpenseRecordController(CardExpenseRecordService cardExpenseRecordService, ConsumptionItemsService consumptionItemsService, ConsumptionCategoryService consumptionCategoryService, OrganizationService organizationService) {
        this.cardExpenseRecordService = cardExpenseRecordService;
        this.consumptionItemsService = consumptionItemsService;
        this.consumptionCategoryService = consumptionCategoryService;
        this.organizationService = organizationService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(CardExpenseRecordEntity::getId);
        entityOrderFields.add(CardExpenseRecordEntity::getPersonName);
        entityOrderFields.add(CardExpenseRecordEntity::getMemberCard);
        entityOrderFields.add(CardExpenseRecordEntity::getPosNumber);
        entityOrderFields.add(CardExpenseRecordEntity::getNumber);
        entityOrderFields.add(CardExpenseRecordEntity::getAmount);
        entityOrderFields.add(CardExpenseRecordEntity::getBalance);
        entityOrderFields.add(CardExpenseRecordEntity::getNo);
        entityOrderFields.add(CardExpenseRecordEntity::getPayServiceId);
        entityOrderFields.add(CardExpenseRecordEntity::getGmtCreate);
    }

    /**
     * 获取全部的 <strong>消费记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardExpenseRecordEntity entity) {
        LambdaQueryChainWrapper<CardExpenseRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardExpenseRecordEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        for (CardExpenseRecordEntity record : page.getRecords()) {
            record.setServiceDepartment(organizationService.getById(consumptionCategoryService.getById(consumptionItemsService.getById(record.getPayServiceId()).getCategoryId()).getDepartment()).getTitle());
            record.setServiceType(consumptionItemsService.getById(record.getPayServiceId()).getName());
            record.setServiceLargeType(consumptionCategoryService.getById(consumptionItemsService.getById(record.getPayServiceId()).getCategoryId()).getTitle());
        }
        return success(page);
    }

    /**
     * 分页获取 <strong>消费记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, CardExpenseRecordEntity entity) {
        LambdaQueryChainWrapper<CardExpenseRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<CardExpenseRecordEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>消费记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        CardExpenseRecordEntity entity = cardExpenseRecordService.getById(id);
        entity.setServiceDepartment(organizationService.getById(consumptionCategoryService.getById(consumptionItemsService.getById(entity.getPayServiceId()).getId()).getDepartment()).getTitle());
        entity.setServiceType(consumptionItemsService.getById(entity.getPayServiceId()).getName());
        entity.setServiceLargeType(consumptionCategoryService.getById(consumptionItemsService.getById(entity.getPayServiceId()).getCategoryId()).getTitle());
        return success(entity);
    }

    /**
     * 获取打印收据凭证的数据信息
     *
     * @param ids 消费记录ID列表信息
     * @return json
     */
//    @PostMapping("getPrintReceiptInfo")
//    public Object getPrintReceiptInfo(@RequestBody List<String> ids) {
//        PrintReceiptVO data = cardExpenseRecordService.getPrintReceiptInfo(ids);
//        return success(data);
//    }

//    /**
//     * 添加一个 <strong>消费记录</strong>
//     *
//     * @param entity 修改后的信息
//     */
//    @PostMapping
//    public Object add(@RequestBody CardExpenseRecordEntity entity) {
//        cardExpenseRecordService.saveCardExpenseRecord(entity);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
//    }
//
//    /**
//     * 修改一个 <strong>消费记录</strong>
//     *
//     * @param id     主键ID
//     * @param entity 修改后的信息
//     */
//    @PutMapping("{id}")
//    public Object update(@PathVariable String id, @RequestBody CardExpenseRecordEntity entity) {
//        entity.setId(id);
//        cardExpenseRecordService.updateCardExpenseRecord(entity);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
//
//    /**
//     * 删除一个 <strong>消费记录</strong>
//     *
//     * @param id 主键ID
//     */
//    @DeleteMapping("{id}")
//    public Object delete(@PathVariable String id) {
//        cardExpenseRecordService.deleteCardExpenseRecord(id);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
//
//    /**
//     * 删除多个 <strong>消费记录</strong>
//     *
//     * @param ids 主键ID列表
//     */
//    @DeleteMapping
//    public Object deleteIds(@RequestBody List<String> ids) {
//        cardExpenseRecordService.deleteCardExpenseRecord(ids);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<CardExpenseRecordEntity> buildLambdaQuery(CardExpenseRecordEntity entity) {
        LambdaQueryChainWrapper<CardExpenseRecordEntity> lambdaQuery = cardExpenseRecordService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), CardExpenseRecordEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonName()), CardExpenseRecordEntity::getPersonName, entity.getPersonName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMemberCard()), CardExpenseRecordEntity::getMemberCard, entity.getMemberCard());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPosNumber()), CardExpenseRecordEntity::getPosNumber, entity.getPosNumber());
        lambdaQuery.like(entity.getAmount() != null, CardExpenseRecordEntity::getAmount, entity.getAmount());
        lambdaQuery.like(entity.getBalance() != null, CardExpenseRecordEntity::getBalance, entity.getBalance());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getNo()), CardExpenseRecordEntity::getNo, entity.getNo());
        lambdaQuery.like(entity.getGmtCreate() != null, CardExpenseRecordEntity::getGmtCreate, entity.getGmtCreate());
        return lambdaQuery;
    }

}
